package com.th.supcom.pds.recipefee.server;

import java.util.List;
import java.util.Map;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.auditprescription.client.model.PdsPrescDetailCombineModel;
import com.th.supcom.pds.common.client.model.OfficeModel;
import com.th.supcom.pds.domain.pojo.Office;
import com.th.supcom.pds.domain.pojo.PdsPrescDetailCombine;
import com.th.supcom.pds.domain.vo.PatientInfo;
import com.th.supcom.pds.domain.vo.PdsSupplyDetailVO;
import com.th.supcom.pds.domain.vo.PdsSupplyVO;
import com.th.supcom.pds.domain.vo.Result;
import com.th.supcom.pds.internal.api.PdsRecipefeeService;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.model.ResultModel;
import com.th.supcom.pds.recipefee.client.service.RecipeFeeRpc;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

/**
 * 收方收费控制层.
 * 
 * @author 杨冲
 */

public class RecipeFeeServiceController extends GwtController implements RecipeFeeRpc
{

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";
    /**
     * 日志记录器.
     */
    private static final Logger s_logger = LoggerFactory.getLogger (RecipeFeeServiceController.class);

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private static final long serialVersionUID = -7252559535751128820L;

    private PdsRecipefeeService m_pdsRecipefeeService;

    /**
     * 默认构造函数.
     */
    public RecipeFeeServiceController ()
    {
    }

    @Override
    public List <OfficeModel> findExecuteOffice (final PdsPrescDetailCombineModel pdsPrescDetailCombine)
                                                                                                        throws UiException
    {
        RecipeFeeServiceController.s_logger.debug ("进入findExecuteOffice方法,入参:");
        List <OfficeModel> modeList = null;
        try
        {
            final PdsPrescDetailCombine pojo = ConvertUtils.convert (PdsPrescDetailCombine.class, pdsPrescDetailCombine);
            final List <Office> orgList = m_pdsRecipefeeService.findExecutiveOffice (pojo);
            modeList = ConvertUtils.convert (OfficeModel.class, orgList);
        }
        catch (final Exception e)
        {
            RecipeFeeServiceController.s_logger.error (e.getMessage ());
            throw new UiException (e.getMessage (), e);
        }
        return modeList;
    }

    @Override
    public List <PdsPrescDetailInfoModel> findPrescDetailInfoByPrescNo (final PdsPrescDetailInfoModel model,
                                                                        String curretnDispensary) throws UiException

    {

        RecipeFeeServiceController.s_logger.debug ("进入findPrescDetailInfoByCardNo方法,入参:");
        // 通过主处方得到处方明细
        PdsSupplyVO pdsSupplyVo = ObjectConvertUtils.getPdsSupply (model);
        List <PdsSupplyDetailVO> pdsSupplyDetailVOs = m_pdsRecipefeeService.findPrescDetailByPrescNo (pdsSupplyVo,
                                                                                                      curretnDispensary);
        final List <PdsPrescDetailInfoModel> precDetails = ObjectConvertUtils.getPrescDetailModel (pdsSupplyDetailVOs);
        return precDetails;
    }

    @Override
    public ResultModel getPrescByPrescNoOrCardNo (Map <String, Object> map) throws UiException
    {
        ResultModel resultModel = new ResultModel ();
        try
        {
            // 包括患者信息和主处方信息
            Result result = m_pdsRecipefeeService.findPrescByPrescNoOrCard (map);
            if (result == null)
            {
                return null;
            }
            // 患者信息
            PatientInfo patientIno = result.getPatientInfo ();
            PatientInfoModel medicareCardAllInfoModel = ConvertUtils.convert (PatientInfoModel.class, patientIno);
            // 主处方信息
            List <PdsSupplyVO> pdsSupplysList = result.getPdsSupplyVOs ();
            List <PdsPrescDetailInfoModel> pdsPrescDetailInfoModellist = ObjectConvertUtils.getPrescModels (pdsSupplysList);
            resultModel.setPatientInfoModel (medicareCardAllInfoModel);
            resultModel.setPdsPrescDetailInfoModellist (pdsPrescDetailInfoModellist);
        }
        catch (Exception e)
        {
            throw new UiException (e.getMessage (), e);
            // 将vo转化为model自定义模式
            // if (list.isEmpty ())
            // {
            // return new ArrayList <InpPhamApplyDetailModel> ();
            // }
            // Mapper mapper = new DozerBeanMapper ();
            // List <InpPhamApplyDetailModel> result = new ArrayList
            // <InpPhamApplyDetailModel> ();
            // for(InpPhamApplyDetailVO vo:list)
            // {
            // result.add (mapper.map (vo, InpPhamApplyDetailModel.class));
            // }
            // return result;
        }

        return resultModel;
    }

    @Override
    public void modifyWindow (final List <PdsPrescDetailInfoModel> prescList, final String dispensaryCode,
                              final Long window) throws UiException
    {
        RecipeFeeServiceController.s_logger.debug ("进入modifyWindow方法,入参:");
        try
        {
            final List <PdsSupplyVO> pcList = ConvertUtils.convert (PdsSupplyVO.class, prescList);
            m_pdsRecipefeeService.modifyWindows (pcList, window, dispensaryCode);
        }
        catch (final Exception e)
        {
            RecipeFeeServiceController.s_logger.error (e.getMessage ());
            throw new UiException (e.getMessage (), e);
        }

    }

    @Override
    public Map <String, Object> confrimRecipe (final List <PdsPrescDetailInfoModel> prescList,
                                               final List <PdsPrescDetailInfoModel> prescdetailList,
                                               Map <String, Object> map) throws UiException
    {
        // 表中所有选择的主记录
        final List <PdsSupplyVO> pdsSupplys = ObjectConvertUtils.getPdsSupplys (prescList);
        // 表中所有的明细
        final List <PdsSupplyDetailVO> pdsSupplyDetails = ObjectConvertUtils.getPdsSupplyDetails (prescdetailList);
        map.put ("patientId", pdsSupplys.get (0).getPatientId ());
        try
        {
            // 持久化数据服务
            map = m_pdsRecipefeeService.recipeFee (pdsSupplys, pdsSupplyDetails, map);
            @SuppressWarnings ("unchecked")
            List <PdsSupplyDetailVO> list = (List <PdsSupplyDetailVO>) map.get ("prescDetails");
            List <PdsPrescDetailInfoModel> detailList = ObjectConvertUtils.getPrescDetailModel (list);
            map.put ("prescDetails", detailList);
            return map;
        }
        catch (Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }
    }

    public void setPdsRecipefeeService (final PdsRecipefeeService pdsRecipefeeService)
    {

        m_pdsRecipefeeService = pdsRecipefeeService;
    }

    @Override
    public Integer verifyStock (final String deptNo, final String phamcode) throws UiException
    {
        RecipeFeeServiceController.s_logger.debug ("进入verifyStock方法,入参:");
        final PdsPrescDetailCombine pdsprescDetailCombine = new PdsPrescDetailCombine ();
        pdsprescDetailCombine.setPerformedBy (deptNo);
        pdsprescDetailCombine.setPhamCode (phamcode);
        Integer stock = 0;
        try
        {
            stock = m_pdsRecipefeeService.findDrugStockByExample (pdsprescDetailCombine);
        }

        catch (final Exception e)
        {
            RecipeFeeServiceController.s_logger.error (e.getMessage ());
            throw new UiException (e.getMessage (), e);
        }
        return stock;

    }
}
